/**
 * @公司:公约树
 * @位置:苏州
 * @Classname Anime04playToAndAnimeInit
 * @Date 2022-06-24 下午 10:56
 * @Created by hanWanLu
 * @auther 中文:韩万路
 */


package com.animeJavaExample;

import com.animeJava.core.AnimeJava;
import com.animeJava.core.EasingData;
import com.animeJava.dao.animeEvent.AnimeRender;
import com.animeJava.mode.PositionVarModel;
import com.animeJava.mode.animeModel.AnimeHoverProperty;
import com.animeJava.mode.animeModel.AnimeProperty;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.util.List;
import java.util.Map;

/**
 * 动画播放到什么地方与初始化动画的地方
 */
public class Anime04playToAndAnimeInit extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setWidth(500);
        primaryStage.setHeight(500);

        AnchorPane root = new AnchorPane();
        primaryStage.setScene(new Scene(root));

        Button box = new Button("");
        root.getChildren().add(box);
        box.setPrefWidth(80);
        box.setPrefHeight(80);
        box.setLayoutX(40);
        box.setLayoutY(40);

        AnimeHoverProperty animeProperty = new AnimeHoverProperty();
        animeProperty.setPlayOneKey(true);
        animeProperty.setDuration(800);
        animeProperty.inPosition(40D,40D);
        animeProperty.setEasing(new EasingData(0.34,1.55,0.40,0.88));
//        AnimeProperty animeProperty = new AnimeProperty();
//        animeProperty.addKeyFrames(0,0D);
//        animeProperty.addKeyFrames(100,1D);
//        animeProperty.setDuration(100);
        AnimeJava animeJava = new AnimeJava(animeProperty);
        animeJava.update(new AnimeRender() {
            @Override
            public boolean run(Double timeTo, Map<String, PositionVarModel> animeInfo, List<Double> data) {
                if(data.get(0) != null){
                    Platform.runLater(()->{
                        box.setLayoutX(data.get(0));
                    });
                }
                if(data.get(1) != null){
                    Platform.runLater(()->{
                        box.setLayoutY(data.get(1));
                    });
                }
                return true;
            }
        });

        Button toggle = new Button("切换滑动位置");
        root.getChildren().add(toggle);

        final int[] is ={0};
        toggle.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("切换-------------------");

                if(is[0] == 0){
                    animeJava.playTo(300D, 40D);
//                    animeJava.play();
                    box.setText("右");
                }else if(is[0] == 1){
                    animeJava.playTo(300D, 300D);
//                    animeJava.playFlashback();
                    box.setText("右下");
                }else if(is[0] == 2){
                    animeJava.playTo(40D, 300D);
                    box.setText("左下");
                }else if(is[0] == 3){
                    animeJava.playTo(40D, 40D);
                    box.setText("原位");
                }
                is[0]++;
                if(is[0] >= 4){
                    is[0] = 0;
                }
            }
        });
        primaryStage.show();
    }
}
